# coding: UTF-8
# Filename : common.py
# Company : imedia
# Author : hui
# -*-coding:utf-8 -*-

from django.http import HttpResponse
import httputils, xml_tpl, err_tpl, const, utils, mysql, log
import os

#请求验证
def validate(request):
    md5_param = httputils.post(request, 'md5_param')
    secret = httputils.post(request, 'secret')
    if const.validate and not httputils.do_check(request, md5_param, secret):
        return response(err_tpl.validate_error)

#请求验证get请求验证
def validate_get(request):
    md5_param = httputils.get(request, 'md5_param')
    secret = httputils.get(request, 'secret')
    if const.validate and not httputils.do_check_get(request, md5_param, secret):
        return response(err_tpl.validate_error)
    
#HTTP响应
def response(data):
    response = HttpResponse(mimetype='application/xml charset=UTF-8') 
    response.write(xml_tpl.xml_head + data)
    return response

def compare_ids_str(compare_type,  cus_id):
    sql = '''select id from chain_compare where type=%s and cus_id=%s'''
    param = [int(compare_type), int(cus_id)]
    result = mysql.doSearch(sql, param)
    
    ids = []
    for obj in result:
        ids.append(str(obj[0]))
    return ','.join(ids)
    
#image生成绝对路径
def image_path(img_str, ratio = '320x480', is_thumb = False):
    if img_str == None: return ""
    if ratio == None or ratio.strip() == '': ratio = '320x480'
    ratio = ratio + '/'
    thumb = 'thumb/'
    parent_dir = '/upload'
    i = 0
    image = ''
    img_str = str(img_str)
    for img in img_str.split(';'):
        if len(img.strip()) > 0:
            sp = '' if i == 0 else ';'
            img = img.replace(parent_dir, '')
            if is_thumb:
                image += sp + const.domain + parent_dir + os.sep + ratio + os.sep + thumb + img
            else:    
                image += sp + const.domain + parent_dir + os.sep + ratio + img
            i += 1
    return image

#为刘洋而写
def fuck_liuyang(img_str, ratio = '320x480', is_thumb = False):
    image_str = image_path(img_str, ratio, is_thumb)
    images = image_str.split(';')
    return images[0]
